﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;
using StateGrid;
using System.Xml;
using StateGrid.GlobalVariables;

namespace StateGrid.DLBK
{
    /// <summary>
    /// Summary description for DLBK_DAO
    /// </summary>
    public class DLBK_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();

        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();

        /// <summary>
        /// 获取所有电力报刊分类
        /// </summary>
        /// <returns></returns>
        public IList<DLBK_FL> GetAllDLBK_FL()
        {
            return sqlMapper.QueryForList<DLBK_FL>("SelectAllDLBK_FL", null);
        }

        /// <summary>
        /// 获取所有电力报刊篇名信息
        /// </summary>
        /// <returns></returns>
        public IList<DLBK_PM> GetAllDLBK_PM()
        {
            return sqlMapper.QueryForList<DLBK_PM>("SelectAllDLBK_PM", null);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public DLBK_FL GetDLBK_FLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<DLBK_FL>("SelectDLBK_FLByFlbm", flbm);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flmc">分类名称</param>
        /// <returns></returns>
        public DLBK_FL GetDLBK_FLByFlmc(String flmc)
        {
            return sqlMapper.QueryForObject<DLBK_FL>("SelectDLBK_FLByFlmc", flmc);
        }

        /// <summary>
        /// 获取指定分类编码篇名数量
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <returns></returns>
        public int GetCountSomeDLBK_PM(String flbm)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();
            if(null==flbm)
            {
                flbm="%";
            }
            else{
            flbm+='%';
            }
            para.Add("flbm", flbm);
            return sqlMapper.QueryForObject<int>("CountSomeDLBK_PM", para);
        }


        /// <summary>
        /// 获取指定索引编码、年份、期号的片名数量
        /// </summary>
        /// <param name="flbm">索引编码</param>
        /// <param name="start">起始位置</param>
        /// <param name="end">结束位置</param>
        /// <returns></returns>
        public IList<DLBK_PM> GetSomeDLBK_PM(String flbm, int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

           if(null==flbm)
            {
                flbm="%";
            }
            else{
            flbm+='%';
            }
            para.Add("flbm", flbm);
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<DLBK_PM>("SelectSomeDLBK_PM", para);
        }

        /// <summary>
        /// 获取指定ID号篇名信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public DLBK_PM GetDLBK_PMByID(int  id)
        {
            return sqlMapper.QueryForObject<DLBK_PM>("SelectDLBK_PMByID", id);
        }

        /// <summary>
        /// 插入一条分类信息
        /// </summary>
        /// <param name="dlbk_fl">分类信息</param>
        /// <returns></returns>
        public Boolean InsertDLBK_FL(DLBK_FL dlbk_fl)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertDLBK_FL", dlbk_fl);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 插入一条篇名信息
        /// </summary>
        /// <param name="dlbk_pm">篇名信息</param>
        /// <returns></returns>
        public Boolean InsertDLBK_PM(DLBK_PM dlbk_pm)
        {
            Boolean flag = true;
            OperationLogItem operationLog = this.GetOperationLogItem(dlbk_pm, "INSERT", null);
            try
            {
                sqlMapper.Insert("InsertDLBK_PM", dlbk_pm);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 更新一条分类信息
        /// </summary>
        /// <param name="dlbk_fl">要更新的分类信息</param>
        /// <returns></returns>
        public Boolean UpdateDLBK_FL(DLBK_FL dlbk_fl)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateDLBK_FL", dlbk_fl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;

        }
        /// <summary>
        /// 更新一条篇名信息
        /// </summary>
        /// <param name="dlbk_pm">要更新的篇名信息</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public Boolean UpdateDLBK_PM(DLBK_PM dlbk_pm, String uname)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateDLBK_PM", dlbk_pm);
            if (back <= 0)
            {
                flag = false;
            }
            if (flag)
            {
                OperationLogItem operationLog = this.GetOperationLogItem(dlbk_pm, "UPDATE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            return flag;
        }

        /// <summary>
        /// 删除指定分类编码的分类信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <returns></returns>
        public Boolean DeleteDLBK_FL(String flbm)
        {
            Boolean flag = true;
            int back = sqlMapper.Delete("DeleteDLBK_FLByFlbm", flbm);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 按ID删除篇名信息
        /// </summary>
        /// <param name="id">指定的ID</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>

        public Boolean DeleteDLBK_PMByID(int id, String uname)
        {
            int result = 0;

            DLBK_PM dlbkPM = this.GetDLBK_PMByID(id);

            OperationLogItem operationLog = null;
            result  = sqlMapper.Delete("DeleteDLBK_PMByID", id);
            if(result>0)
            {
                operationLog = this.GetOperationLogItem(dlbkPM, "DELETE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="zwqkPM">电力报刊刊信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(DLBK_PM dlbkPM, String opType, String uname)
        {
            if (null == dlbkPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "DLBK_PM";
            //全文页数
            try
            {
                operationLogItem.opPageCount = 0;
            }
            catch (Exception)
            {
            }

            operationLogItem.opObjectID = "电力报刊";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "电力报刊";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + dlbkPM.flbm + "','" + dlbkPM.bt + "','" + dlbkPM.zz + "','" + dlbkPM.rq + "','" + dlbkPM.bh + "','" + dlbkPM.lm + "','" + dlbkPM.zw + "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }


      

        public DLBK_DAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}